﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;
using System.Web.Mvc;

namespace BlackBird.DataAccess
{
	public static class FuJianDataAccess
	{

		static public int SaveFuJianItemToDB(string Name, string Path, string MineType, string userId)
		{
			using (var context = new blackbirdEntities())
			{
				var obj = new fujian { Name = Name, Path = Path, MineType = MineType, User_idUser = userId,Date = DateTime.Now };
				context.fujian.AddObject(obj);
				context.SaveChanges();

				return obj.idFujian;
			}
		}

		static public object UpdateInfoToParent(UploadFile uploadInfo,string fileName, int FuJianID, user us)
		{
			switch (uploadInfo.type)
			{
				case SupportTypes.ExamplContract:
					{
						var heTongID = ContractDataAccess.NewSampleHeTong(FuJianID, us.idUser, fileName);
						return new { Id = heTongID };
					}

				case SupportTypes.ProjectContract:
					{
						var heTongID = ContractDataAccess.NewProjectHeTong(int.Parse(uploadInfo.parentId),FuJianID, us.idUser, fileName);
						return new { Id = heTongID };
					}

				case SupportTypes.BiJiaDan:
					{
						var id = FuJianDataAccess.NewBiJiaDanAttachedFile(int.Parse(uploadInfo.parentId), FuJianID);
						return new { Id = id };
					}
				case SupportTypes.Income:
					{
					}
					break;

				case SupportTypes.Expend:
					{
					}
					break;

				case SupportTypes.Event:
					{
					}
					break;

				default:

					throw new NotImplementedException("Not support types");
			}

			return true;
		}

		static public fujian GetFuJianInfoByID(int id)
		{
			using (var bbDB = new blackbirdEntities())
			{
				return bbDB.fujian.FirstOrDefault(v => v.idFujian == id);
			}
		}

		/// <summary>
		/// 保存比价单的附件
		/// </summary>
		/// <param name="BiJiaDanId"></param>
		/// <param name="FuJianID"></param>
		/// <returns></returns>
		public static int NewBiJiaDanAttachedFile(int BiJiaDanId, int FuJianID)
		{
			using (var context = new blackbirdEntities())
			{
				var bijiadan_fujian = new bijiadan_fujian { Fujian_idFujian = FuJianID, Bijiandan_idBijiandan = BiJiaDanId };
				context.bijiadan_fujian.AddObject(bijiadan_fujian);
				context.SaveChanges();
				return bijiadan_fujian.id;
			}
		}

		/// <summary>
		/// 获得比价单的附件
		/// </summary>
		/// <param name="BiJiaDanId"></param>
		/// <returns></returns>
		public static List<AttachedFile> GetBiJiaDanAttachedFile(int BiJiaDanId, Controller ctrl)
		{
			List<AttachedFile> attachedFiles = null;
			using (var context = new blackbirdEntities())
			{
				var fujian_relations = from f in context.bijiadan_fujian
									   where f.Bijiandan_idBijiandan == BiJiaDanId
									   select f;

				var fujians = fujian_relations.Select<bijiadan_fujian, fujian>(v => v.fujian );

				var attachedfiles = fujians.ToList().Select<fujian,AttachedFile>(v => ContractDataAccess.BuildAttachedFileFromFujian(v, ctrl) );

				attachedFiles = attachedfiles.ToList();
			}

			return attachedFiles;
		}
	}
}